local card_extension = Package:new("lordcards", Package.CardPack)
card_extension.extensionName = "sifu_heg"
local H = require "packages/ol_hegemony/util"
local U = require "packages/utility/utility"
local S = require "packages/sifu_heg/SeaFunc"
card_extension.game_modes_whitelist = {"zhonger_heg_mode"}
Fk:loadTranslationTable{
["lordcards"]=  "君主装备",
}
local dragonPhoenixSkill = fk.CreateTriggerSkill{--飞龙夺凤
    name = "#dragon_phoenix_skill",
    attached_equip = "dragon_phoenix",
    events = {fk.TargetSpecified, fk.EnterDying},
    can_trigger = function(self, event, target, player, data)
      if not player:hasSkill(self) then return end
      if event == fk.TargetSpecified then
        if target == player and data.card and data.card.trueName == "slash" then
          return not player.room:getPlayerById(data.to):isNude()
        end
      else
        return data.damage and data.damage.from == player and not target:isKongcheng() and player.room.logic:damageByCardEffect() and data.damage.card.trueName == "slash"
      end
    end,
    on_cost = function(self, event, target, player, data)
      local prompt = event == fk.TargetSpecified and "#dragon_phoenix-slash::" .. data.to or "#dragon_phoenix-dying::" .. target.id
      return player.room:askForSkillInvoke(player, self.name, data, prompt)
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      room:notifySkillInvoked(player, "dragon_phoenix", "control")
      -- room:setEmotion(player, "./packages/hegemony/image/anim/dragon_phoenix")
      if event == fk.TargetSpecified then
        local to = player.room:getPlayerById(data.to)
        room:askForDiscard(to, 1, 1, true, self.name, false, ".", "#dragon_phoenix-invoke")
      else
        local card = room:askForCardChosen(player, target, "h", self.name)
        room:obtainCard(player, card, false, fk.ReasonPrey)
      end
    end,
  }
  Fk:addSkill(dragonPhoenixSkill)
  
  local dragonPhoenix = fk.CreateWeapon{
    name = "dragon_phoenix",
    suit = Card.Spade,
    number = 2,
    attack_range = 2,
    equip_skill = dragonPhoenixSkill,
  }
  card_extension:addCard(dragonPhoenix)
  Fk:loadTranslationTable{
    ["dragon_phoenix"] = "飞龙夺凤",
    [":dragon_phoenix"] = "装备牌·武器<br/><b>攻击范围</b>：２ <br/><b>武器技能</b>：①当你使用【杀】指定目标后，你可令目标弃置一张牌。②当一名角色因执行你使用的【杀】的效果而受到你造成的伤害而进入濒死状态后，你可获得其一张手牌。",
    ["#dragon_phoenix_skill"] = "飞龙夺凤",
    ["#dragon_phoenix-slash"] = "飞龙夺凤：你可令 %dest 弃置一张牌",
    ["#dragon_phoenix-dying"] = "飞龙夺凤：你可获得 %dest 一张手牌",
    ["#dragon_phoenix-invoke"] = "受到“飞龙夺凤”影响，你需弃置一张牌",
  }

  local peaceSpellSkill = fk.CreateTriggerSkill{--太平要术
    name = "#peace_spell_skill",
    attached_equip = "peace_spell",
    frequency = Skill.Compulsory,
    events = {fk.DamageInflicted},
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(self) and data.damageType ~= fk.NormalDamage
    end,
    on_use = function(self, event, target, player, data)
      player.room:notifySkillInvoked(player, "peace_spell", "defensive")
      return true
    end,
  }
  local peace_spell_maxcards = fk.CreateMaxCardsSkill{
    name = "#peace_spell_maxcards",
    correct_func = function(self, player)
      if player:hasSkill("#peace_spell_skill") then
        if player.kingdom == "unknown" then
          return 1
        else
          local num = H.getSameKingdomPlayersNum(Fk:currentRoom(), player)
          return (num or 0) + #player:getPile("heavenly_army")
        end
      end
    end,
  }
  peaceSpellSkill:addRelatedSkill(peace_spell_maxcards)
  Fk:addSkill(peaceSpellSkill)
  local peace_spell = fk.CreateArmor{
    name = "peace_spell",
    suit = Card.Heart,
    number = 3,
    equip_skill = peaceSpellSkill,
    on_uninstall = function(self, room, player)
      Armor.onUninstall(self, room, player)
      if not player.dead and self.equip_skill:isEffectable(player) then
        room:notifySkillInvoked(player, "peace_spell", "drawcard")
        player:drawCards(2, self.name)
        if player.hp > 1 then
          room:loseHp(player, 1, self.name)
        end
      end
    end,
  }
  card_extension:addCard(peace_spell)
  Fk:loadTranslationTable{
    ["peace_spell"] = "太平要术",
    [":peace_spell"] = "装备牌·防具<br /><b>防具技能</b>：锁定技，①当你受到属性伤害时，你防止此伤害。②你的手牌上限+X（X为与你势力相同的角色数）。③当你失去装备区里的【太平要术】后，你摸两张牌，然后若你的体力值大于1，你失去1点体力。",
  }
local luminousPearlSkill = fk.CreateActiveSkill{
    name = "ze_luminous_pearl_skill",
    attached_equip = "ze_luminous_pearl",
    can_use = function(self, player)
      return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,
    target_num = 0,
    min_card_num = 1,
    max_card_num = function()
      return Self.maxHp
    end,
    card_filter = function(self, to_select, selected)
      return #selected < Self.maxHp and not Self:prohibitDiscard(Fk:getCardById(to_select)) and Fk:getCardById(to_select).name ~= "ze_luminous_pearl"
    end,
    on_use = function(self, room, effect)
      local from = room:getPlayerById(effect.from)
      room:notifySkillInvoked(from, "ze_luminous_pearl", "drawcard")
      room:throwCard(effect.cards, self.name, from, from)
      if not from.dead then
        from:drawCards(#effect.cards, self.name)
      end
    end
  }
  local luminousPearlTrig = fk.CreateTriggerSkill{
    name = "#ze_luminous_pearl_trigger",
    refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill},
    can_refresh = function(self, event, target, player, data)
      return player == target and (data == Fk.skills["hs__zhiheng"] or data == Fk.skills["ld__lordsunquan_zhiheng"] ) and table.find(player:getEquipments(Card.SubtypeTreasure), function(cid)
        return Fk:getCardById(cid).name == "ze_luminous_pearl"
      end)
    end,
    on_refresh = function(self, event, target, player, data)
      player.room:handleAddLoseSkills(player, event == fk.EventAcquireSkill and "-ze_luminous_pearl_skill" or "ze_luminous_pearl_skill", nil, false, true)
    end,
  }
  luminousPearlSkill:addRelatedSkill(luminousPearlTrig)
  Fk:addSkill(luminousPearlSkill)
  
  local luminousPearl = fk.CreateTreasure{
    name = "ze_luminous_pearl",
    suit = Card.Diamond,
    number = 6,
    equip_skill = luminousPearlSkill,
    on_install = function(self, room, player)
      Treasure.onInstall(self, room, player)
      if (player:hasSkill("hs__zhiheng") or player:hasSkill("ld__lordsunquan_zhiheng") ) then room:handleAddLoseSkills(player, "-ze_luminous_pearl_skill", nil, false, true) end
    end,
  }
  Fk:loadTranslationTable{
    ["ze_luminous_pearl"] = "定澜夜明珠",
    [":ze_luminous_pearl"] = "装备牌·宝物<br/><b>宝物技能</b>：锁定技，若你没有〖制衡〗，你视为拥有〖制衡〗；若你有〖制衡〗，取消〖制衡〗的数量限制。",
    ["ze_luminous_pearl_skill"] = "制衡",
    [":ze_luminous_pearl_skill"] = "出牌阶段限一次，你可弃置至多X张牌（X为你的体力上限），然后你摸等量的牌。<font color='grey'><small>此为【制衡（定澜夜明珠）】</small></font>",
  }

  local liulongcanjiaSkill = fk.CreateDistanceSkill{--六龙骖驾
  name = "#liulongcanjiaSkill",
  attached_equip = "liulongcanjia",
  correct_func = function(self, from, to)
    local n = 0
    if from:hasSkill(self) then
      n = n -1
    end
    if to:hasSkill(self) then
      n = n + 1
    end
    return n
  end,
}
local liulongProhibit = fk.CreateProhibitSkill{
  name = "#liulongcanjia_prohibit",
  attached_equip = "liulongcanjia",
  prohibit_use = function(self, player, card)
    return player:hasSkill(liulongcanjiaSkill) and table.contains({Card.SubtypeDefensiveRide, Card.SubtypeOffensiveRide}, card.sub_type)
  end,
}
liulongcanjiaSkill:addRelatedSkill(liulongProhibit)
local liulongcanjia = fk.CreateDefensiveRide{
  name = "liulongcanjia",
  suit = Card.Heart,
  number = 13,
  equip_skill = liulongcanjiaSkill,
  ---@param room Room
  ---@param player ServerPlayer
  on_install = function(self, room, player)
    local cards = player:getEquipments(Card.SubtypeOffensiveRide)
    if #cards > 0 then room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name, nil, true, player.id) end
    DefensiveRide.onInstall(self, room, player)
    room:setPlayerMark(player, "@@liulongcanjia", 1) -- 绷
  end,
  on_uninstall = function(self, room, player)
    DefensiveRide.onUninstall(self, room, player)
    room:setPlayerMark(player, "@@liulongcanjia", 0)
  end,
}
Fk:addSkill(liulongcanjiaSkill)
card_extension:addCard(liulongcanjia)

Fk:loadTranslationTable{
  ["liulongcanjia"] = "六龙骖驾",
  [":liulongcanjia"] = "装备牌·坐骑<br /><b>坐骑技能</b>：锁定技，其他角色与你的距离+1，你与其他角色的距离-1；当【六龙骖驾】移至你的装备区后，你将你的装备区里所有其他坐骑牌置入弃牌堆；你不能使用坐骑牌。",

  ["@@liulongcanjia"] = "六龙骖驾",
}

card_extension:addCards{
luminousPearl, --夜明珠
}

return card_extension
